//
//  InfoViewController.m
//  QIRestorationDemo
//
//  Created by QLY on 2019/6/30.
//  Copyright © 2019 qishare. All rights reserved.
//

#import "InfoViewController.h"
#import "InfoModel.h"
#import "InfoDetailController.h"
@interface InfoViewController () <UITableViewDataSource, UITableViewDelegate,UIDataSourceModelAssociation>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSArray *identifierArray;

@property (nonatomic, strong) NSIndexPath *currentPath;

@end

@implementation InfoViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"列表";
    [self setupViews];
    [self setupDatas];
    //设置恢复标识
}

#pragma mark - Private functions

- (void)setupViews{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.restorationIdentifier = NSStringFromClass(_tableView.class);
    _tableView.estimatedSectionHeaderHeight = 0.0;
    _tableView.estimatedSectionFooterHeight = 0.0;
    _tableView.tableFooterView = nil;
    _tableView.tableHeaderView = nil;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass(UITableViewCell.class)];
    [self.view addSubview:_tableView];
    
}


- (void)setupDatas{
    
    NSArray *array = @[@"appdelegate",@"controller",@"uirestoration",@"restorationId",@"restorationClass",@"view",@"seeyou",@"come on",@"i have no idea",@"陈宫",@"失败",@"似大江一发不收",@"以为平复心中争斗",@"人员翻白浅蓝",@"浪奔流"];
    NSArray *identifierArray = @[@"QiRestoration_0",@"QiRestoration_1",@"QiRestoration_2",@"QiRestoration_3",@"QiRestoration_4",@"QiRestoration_5",@"QiRestoration_6",@"QiRestoration_7",@"QiRestoration_8",@"QiRestoration_9",@"QiRestoration_10",@"QiRestoration_11",@"QiRestoration_12",@"QiRestoration_13",@"QiRestoration_14"];
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (int i = 0; i < array.count; i ++) {
        InfoModel *model = [[InfoModel alloc]init];
        model.title = array[i];
        model.identifier = identifierArray[i];
        [tempArray addObject:model];
    }
    self.dataSource = tempArray.copy;
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class) forIndexPath:indexPath];
    cell.textLabel.text = model.title;
    cell.restorationIdentifier = model.identifier;
    
    return cell;
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _currentPath = indexPath;
    
    InfoModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    InfoDetailController *infoDetail = [[InfoDetailController alloc]init];
    infoDetail.model = model;
    //如果这个类中设置了restorationClass，推测如不实现那个方法，则不会恢复，我们试一下。
    [self.navigationController pushViewController:infoDetail animated:YES];
}


#pragma mark - lazy

- (NSArray *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [[NSArray alloc]init];
    }
    
    return _dataSource;
}

#pragma mark - UIDataSourceModelAssociation
/** 有了UIDataSourceModelAssociation和tableView的restorationIdentifier，这个方法就必须实现，不然会奔溃****/
- (NSString *)modelIdentifierForElementAtIndexPath:(NSIndexPath *)idx inView:(UIView *)view {
    //根据index 返回identifier
    NSString *identifier = nil;
    InfoModel *model = [self.dataSource objectAtIndex:idx.row];
    
    /*
     此方法会在保存时调用两次，idx所返回的数据除了我们选中的行，还会返回一个其他行。 若是采用这种实行映射唯一标识，会出现保存了我们不需要的行的标识，导致恢复滑动位置失效，针对此问题目前笔者尚未有答案。所以在此基础上笔者自己想的解决办法：定义_currentPath追踪当前选中的cell，保存时根据_currentPath保存我们需要的标识，测试中发现可以解决问题。
     if (idx && view) {
       identifier = model.identifier;
    }
    */
    if (idx.row == _currentPath.row && view) {
        identifier = model.identifier;
    }
    //若是不定义_currentPath追踪当前选中的cell.会多保存一个cell，目前尚未有答案。
    return identifier;
}

//此方法 恢复时调用
- (NSIndexPath *)indexPathForElementWithModelIdentifier:(NSString *)identifier inView:(UIView *)view {
    //根据identifier 返回index;
    NSIndexPath *indexPath = nil;

    if (identifier && view) {
        __block NSInteger row = 0;
        [self.dataSource enumerateObjectsUsingBlock:^(InfoModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.identifier isEqualToString:identifier]) {
                row = idx;
                *stop = YES;
            }
        }];
        indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        _currentPath = indexPath;
        NSLog(@"当前选中的数据源对象标识是：%@,对象抬头是:%@",[self.dataSource[indexPath.row] identifier],[self.dataSource[indexPath.row] title]);
    }
    return indexPath;
}

@end
